﻿using System;
using CardLogistics.Web.Components;
using System.Data;
using System.Text;
using CardLogistics.DAL;
using System.IO;
using System.Web;

namespace CardLogistics.Web
{
    public partial class mainfra : BasePage
    {
        protected string UserLoginName = "";
        protected string AfterLog = "未查询到";
        public string CharXVal;
        public string CharSeries;
        public string CharYVal;
        public StringBuilder strli = new StringBuilder();//待审批记录统计
        protected string ConInfo = "";//合同预警信息
        private static readonly CardLogistics.DAL.Sys.Sys_LogData LogDAL = new DAL.Sys.Sys_LogData();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Mid == "100106")
                        ConId.Visible = true;
                    else if (Mid == "100107")
                        OrderId.Visible = true;
                    else
                        HomeRoot(); //设置首页权限
                    ConInfo = GetConInfo();
                    GetAppCount();
                }
                UserLoginName = LoggedUser.USERNAME;
                DataTable dt = LogDAL.GetList(" USERID='" + LoggedUser.ID + "' and LOGCONTENT='登录系统'", "LOGDATE", "LOGDATE");
                if (dt.Rows.Count > 1)
                    AfterLog = DateTime.Parse(dt.Rows[1][0].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            }
            catch
            { }
        }

        /// <summary>
        /// 首页权限设置
        /// </summary>
        private void HomeRoot()
        {
            DataTable dk = CommonData.GetListOrderBy("MenuId='100106' Or MenuId='100107'", "SYS_SYSROLEMENUS", "MenuId", "MenuId", 0);
            if (dk.Rows.Count > 0)
            {
                for (int i = 0; i < dk.Rows.Count; i++)
                {
                    if (dk.Rows[i]["MenuId"].ToString() == "100106")//合同预警
                        ConId.Visible = true;
                    if (dk.Rows[i]["MenuId"].ToString() == "100107")//订单预警
                        OrderId.Visible = true;
                }
            }
        }

        /// <summary>
        /// 获取合同预警信息
        /// </summary>
        /// <returns></returns>
        private string GetConInfo()
        {
            string result = "";
            StringBuilder sbYJ = new StringBuilder();
            sbYJ.Append("select conno,BEDATE,DELIVERYDAYS,c.deTotal,c.OrTotal from  v_purchase_contract c");
            //当入库数量小于合同数量 并且 供货日期小于10天 且不超过30天的预警
            sbYJ.Append(" where OrTotal<deTotal");
            //sbYJ.Append(" and ceil((BEDATE+DELIVERYDAYS)-sysdate) between -30 and 20 ");
            sbYJ.Append(" order by BEDATE ");
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult(sbYJ.ToString());
            if (dt.Rows.Count == 0)
            {
                result = "<font color='red'>暂无合同预警信息</font>";
            }
            else
            {
                foreach (DataRow dr in dt.Rows)
                {
                    result += " 合同编号:<b>" + dr["conno"] + "</b> 生效日期:<b>" + DateTime.Parse(dr["BEDATE"].ToString()).ToString("yyyy-MM-dd") + "</b>";
                    result += " 供货期:<b>" + dr["DELIVERYDAYS"].ToString() + "</b> 天";
                    result += " 合同总数:<b>" + dr["deTotal"].ToString() + "</b> 入库数量:<b>" + dr["OrTotal"].ToString() + "</b>";
                    int noOrd = int.Parse(dr["deTotal"].ToString()) - int.Parse(dr["OrTotal"].ToString());
                    result += " 未入库数量:<b>" + noOrd + "</b> ";
                    result += "<br/>";
                }
            }
            return result;
        }

        /// <summary>
        /// 获取订单预警信息
        /// </summary>
        /// <returns></returns>
        protected string GetOrdInfo()
        {
            string result = "";
            StringBuilder sbYJ = new StringBuilder();
            sbYJ.Append("select t.orderno,t.PConNo,t.CardTotal,t.PutInTotal,ORDERDATA,t.GETDATA,ceil(GETDATA-sysdate) GetDay from v_ordering_cardinfo t where t.Issubmit=5");
            //当入库数量小于订单数量 并且 距到期还有10天 且不超过30天的预警
            sbYJ.Append(" and PutInTotal<CardTotal and ceil(GETDATA-sysdate) between -30 and 10");
            sbYJ.Append(" order by GETDATA ");
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult(sbYJ.ToString());
            foreach (DataRow dr in dt.Rows)
            {
                result += " 订单编号:<b>" + dr["orderno"] + "</b> 合同编号:<b>" + dr["PConNo"] + "</b> ";
                result += " 订单日期:<b>" + DateTime.Parse(dr["ORDERDATA"].ToString()).ToString("yyyy-MM-dd") + "</b>";
                result += " 到货日期:<b>" + DateTime.Parse(dr["GETDATA"].ToString()).ToString("yyyy-MM-dd") + "</b>";
                result += " 订单总数:<b>" + dr["CardTotal"].ToString() + "</b> 入库数量:<b>" + dr["PutInTotal"].ToString() + "</b>";
                if (int.Parse(dr["GetDay"].ToString()) > 0)
                    result += " 距到货期还有:<b>" + dr["GetDay"].ToString() + "</b> 天";
                else
                    result += " 已超过到货货期:<b>" + dr["GetDay"].ToString().Replace("-", "") + "</b> 天";
                result += "<br/>";
            }
            return result;
        }

        /// <summary>
        /// 查询待审批记录数
        /// </summary>
        public void GetAppCount()
        {
            //根据登录用户获取所有未审批信息
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult(" select * from APPROVALINFO where APPSTATUS=0 and USERID='" + LoggedUser.ID + "'");
            strli = new StringBuilder();
            if (dt != null)
            {
                //0=采购需求 1=采购合同 2=卡片订货单 3=卡片验收入库 4=卡片需求单 5=半成品/成品入库 6=半成品/成品需求 7=半成品/成品申领  8=半成品/成品出库 9=半成品/成品发放 10=白卡出库 11=制卡坏卡 12=未售问题卡回收 13=报废入库 14=卡片退货 15半成品/成品调拨入库 16 半成品/成品调拨出库
                string[] typeName = { "采购需求", "采购合同", "卡片订货单", "卡片验收入库", "卡片需求单", "离盒操作","白卡出库", "半成品/成品入库", "半成品/成品需求", "半成品/成品申领", "半成品/成品出库", "半成品/成品发放", "半成品/成品调拨入库", "半成品/成品调拨出库", "制卡坏卡", "未售问题卡", "卡片退货", "报废入库" };//类型名称
                int[] infoType = { 0, 1, 2, 3, 4, 17, 10, 5, 6, 7, 8, 9, 15, 16, 11, 12, 14, 13 };//类型标识
                DataTable dtMenu = CardLogistics.DAL.DbHelperSQL.GetQueryResult("select * from sys_menus where menuid in(select menuid from sys_sysrolemenus where Status=1 and RoleId='" + LoggedUser.ROLEID + "') and urlwinformname is not null ");
                for (int i = 0; i < typeName.Length; i++)
                {
                    if (dt.Select(" INFOTYPE=" + infoType[i]).Length > 0)
                    {
                        strli.Append("<li><a href='" + dtMenu.Select(" menuname='" + typeName[i] + "'")[0]["urlwinformname"].ToString() + "?mid=" + dtMenu.Select(" menuname='" + typeName[i] + "'")[0]["menuid"].ToString() + "'>" + typeName[i] + "<b style='font:14px; color:Red;'>" + dt.Select(" INFOTYPE=" + infoType[i]).Length.ToString() + "</b>条</a></li>");
                    }
                    else
                    {
                        strli.Append("<li><a href='#'>" + typeName[i] + "<b style='font:14px; color:Red;'>0</b>条</a></li>");
                    }
                }
            }
        }

        protected void lbtnDownLoad_Click(object sender, EventArgs e)
        {
            string fileName = "报表插件.exe";//客户端保存的文件名
            string filePath = Server.MapPath("MainPage/File/DownLoad/grbsctl5.exe");//路径
            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

    }
}